<?php

class DishController extends Zend_Controller_Action
{

	protected $_application;
	
	
	
    public function init()
    {
    	$this->view->headTitle("Dish");
    	
    	
    }

    function preDispatch()
    {
    	
    }
    
    public function indexAction()
    {

    	if (Model_Role::IsDenied()){
    		$this->_redirect('/error/deny');
    	}
    	
        // action body
    	$dishMapper=new Model_DishMapper();
    	$data=$dishMapper->getAllDishes();
    	$this->view->dishes=$data;
    }
    public function detailAction()
    {
    	$id=$this->_getParam("id");
    	try {
    		$dishMapper=new Model_DishMapper();
    		$dish=$dishMapper->getDish($id);
    		$this->view->dish=$dish;
    		$this->view->id=$id;
    		$this->view->name=$dish->getName();
    		$this->view->description=$dish->getDescription();
    		$this->view->image=$dish->getImage();
    		$loc_dishMapper=new Model_DishLocationMapper();
    		$locs=$loc_dishMapper->getAllLocationOfDish($id);
    		$this->view->locs=$locs;
    		
    		if ($dish->getCategory()!=null)
    			$this->view->category=$dish->getCategory()->getName();
    		$this->view->error=false;
    	}
    	catch(Exception $e) {
    		$this->view->error=true;
    	}
    	
    }
    public function createAction()
    {
    	$catM=new Model_CategoryMapper();
    	$cats=$catM->getAllCategories();
    	$this->view->cats=$cats;
    	
    	$config = Zend_Registry::get('config');  
    	$roles = $config->acl->roles;
    	$this->_addRoles($roles);
    	
    }
    protected function _addRoles($roles)
    {
    	foreach ($roles as $name=>$parents) {  
    		if (!$this->hasRole($name)) {   
    			if (empty($parents)) {
    				$parents = null;
    			} else {
    				$parents = explode(',', $parents);
    			}
    			$this->addRole(new Zend_Acl_Role($name), $parents);
    		}
    	}
    }
    
    public function createprocessAction()
    {
    	 if ($this->_request->isPost())
    	 {
    	 	$name=$this->_getParam("dish-name");
    	 	$cat=$this->_getParam("dish-category");
    	 	$image=$this->_getParam("dish-image");
    	 	$des=$this->_getParam("dish-description");
    	 	
    	 	$error="";
    	 	
    	 	$dish=new Model_Dish();
    	 	$cc=new Model_Category();
    	 	$cc->setId($cat);
    	 	$dish->setName($name)->setCategory($cc)->setDescription($des)->setImage($image);
    	 	$dishM=new Model_DishMapper();
    	 	$result=$dishM->createDish($dish);
    	 	
    	 	if ($result=="error")
    	 	{
    	 		$this->_redirect('/dish/create');
    	 	}
    	 	else
    	 	{
    	 		$this->_redirect('/dish/index');
    	 	}
    	 	
    	 	
    	 }
    }
    
    public function managerAction()
    {
    	if (Model_Role::IsDenied()){
    		$this->_redirect('/error/deny');
    	}
    	 
    	// action body
    	$dishMapper=new Model_DishMapper();
    	$data=$dishMapper->getAllDishes();
    	$this->view->dishes=$data;
    }
    
    public function editAction()
    {
    	$id=$this->_getParam("id");
    	try {
    		$dishMapper=new Model_DishMapper();
    		$dish=$dishMapper->getDish($id);
    		$this->view->dish=$dish;
    		$this->view->id=$id;
    		$this->view->name=$dish->getName();
    		$this->view->description=$dish->getDescription();
    		$this->view->image=$dish->getImage();
    		
    		$catM=new Model_CategoryMapper();
    		$cats=$catM->getAllCategories();
    		$this->view->cats=$cats;
    		
    		if ($dish->getCategory()!=null)
    		{
    			$this->view->category=$dish->getCategory()->getName();
    			$this->view->categoryId=$dish->getCategory()->getId();
    		}
    			
    		$this->view->error=false;
    	}
    	catch(Exception $e) {
    		$this->view->error=true;
    	}
    
    	 
    }
    
    public function editprocessAction(){
    		$name=$this->_getParam("dish-name");
    		$id=$this->_getParam("id");
    	 	$cat=$this->_getParam("dish-category");
    	 	$image=$this->_getParam("dish-image");
    	 	$des=$this->_getParam("dish-description");
    	 	
    	 	$error="";
    	 	
    	 	$dish=new Model_Dish();
    	 	$cc=new Model_Category();
    	 	$cc->setId($cat);
    	 	$dish->setName($name)->setCategory($cc)->setDescription($des)->setImage($image)->setId($id);
    	 	$dishM=new Model_DishMapper();
    	 	$result=$dishM->updateDish($dish);
    	 	
    	 	if ($result=="error")
    	 	{
    	 		$this->_redirect('/dish/edit?id='.$id);
    	 	}
    	 	else
    	 	{
    	 		$this->_redirect('/dish/index');
    	 	}
    }
    
    public function removeAction()
    {
    	$id=$this->_getParam("id");
    	$dishM=new Model_DishMapper();
    	$result=$dishM->removeDish($id);
    	
    	$this->_redirect('/dish/index');
    	
    }

}

